<?php

// Class dependencies
require_once __DIR__.DS.'blgRegister.php';

/**
 * Implements a class for registering views wich can be pages, layouts, css, components, etc
 * @author Pablo
 * @since 1.0
 */

class blgViewsRegister extends blgRegister {
	
	function __construct(){
		parent::__construct();
		$this->makeFirstColUnique= TRUE;
	}
	
	/**
	 * Register a view
	 * @param $view_name
	 * @param $view_path
	 * @return Boolean
	 * @author Pablo
	 * @since 1.0
	 * @see engine/clases/blgRegister::register()
	 */
	function register($view_name, $view_path, $type= NULL, $handler= NULL){
		if(empty($view_name) || empty($view_path))
			return FALSE;
			
		if(!is_file($view_path)){
			trigger_error('Registering '.$view_path.' failed. $view_path not found.', E_USER_WARNING);
			return FALSE;
		}
			
		return parent::register(array('name' => $view_name, 'path' => $view_path, 'type' => $type));
	}

	/**
	 * Tells if a $needle is located in a $column of the register
	 * @param $column
	 * @param $needle
	 * @return Boolean
	 * @author Pablo
	 * @since 1.0
	 */
	function is_registered($column, $needle){
		foreach($this->table as $row){
			if($row[$column]==$needle)
				return TRUE;					
		}
		
		return FALSE;
	}
	
	/**
	 * Tells if a page is registed
	 * @param $page_name View_name/Page_name registed
	 * @return Boolean
	 * @author Pablo
	 * @since 1.0
	 */	
	function page_exists($pages){
		if(empty($pages))
			$pages= array(EMPTY_PAGE);
		
		$page_names= array();
		foreach($pages as $page){
			$page_name= (empty($page_name)?$page:$page_name.'/'.$page);
			$page_names[]= $page_name;
		}
	
		$params= array();
		foreach (array_reverse($page_names) as $i => $page_name){
			if($this->is_registered('name', $page_name) && $this->type($page_name)=='page'){
				$result= new stdClass();				
				$result->page= $page_name;
				$result->pages= $pages;
				$result->params= array_reverse($params);
				return $result;			
			}
			$params[]= array_pop($pages);			
		}
		
		return FALSE;		
	}
	
	/**
	 * Tells if a page is registed
	 * @param $page_name View_name/Page_name registed
	 * @return Boolean
	 * @author Pablo
	 * @since 1.0
	 */	
	function action_exists($action_name){
		return ( $this->is_registered('name', $action_name) && $this->type($action_name)=='action');		
	}	

	/**
	 * Tells if a CSS file is registed
	 * @param $page_name View_name/Page_name registed
	 * @return Boolean
	 * @author Pablo
	 * @since 1.0
	 */	
	function css_exists($css_name){
		return ( $this->is_registered('name', $css_name) && $this->type($css_name)=='css');		
	}

	/**
	 * Tells if a JS file is registed
	 * @param $page_name View_name/Page_name registed
	 * @return Boolean
	 * @author Pablo
	 * @since 1.0
	 */	
	function js_exists($js_name){
		return ( $this->is_registered('name', $js_name) && $this->type($js_name)=='js');		
	}	
	
	/**
	 * Tells if a component is registed
	 * @param $component_name View_name/Component_name registed
	 * @return Boolean
	 * @author Pablo
	 * @since 1.0
	 */	
	function component_exists($component_name){
		return ( $this->is_registered('name', $component_name) && $this->type($component_name)=='component');		
	}
	
	/**
	 * Tells if a component is registed
	 * @param $component_name View_name/Component_name registed
	 * @return Boolean
	 * @author Pablo
	 * @since 1.0
	 */	
	function file_exists($file_url){
		return blg_file_exists($file_url);
	}		
	
	/**
	 * For a given $view_name, it returns the path corresponding
	 * @param $view_name
	 * @return string | FALSE
	 * @author Pablo
	 * @since 1.0
	 */
	function path($view_name){		
		$view_name= str_replace(blg_get_site_url(), '', $view_name);		
		
		foreach($this->table as $row){
			if($row['name']==$view_name)
				return $row['path'];
		}
		
		return FALSE;
	}
	
	/**
	 * For a given $view_name, it returns the type corresponding
	 * @param $view_name
	 * @return string | FALSE
	 * @author Pablo
	 * @since 1.0
	 */
	function type($view_name){
		foreach($this->table as $row){
			if($row['name']==$view_name)
				return $row['type'];
		}
		
		return FALSE;
	}		
	
	/**
	 * For a given $path, it returns the view(s) corresponding
	 * @param $path
	 * @return Array
	 * @author Pablo
	 * @since 1.0
	 */
	function view($path){
		$return = array();
		
		foreach($this->table as $row){
			if($row['path']==$path)
				$return[]= $row['name'];
		}
		
		return $return;
	}	
}